<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/18 0018
 * Time: 下午 14:14
 */
namespace backend\models\c2\entity;
use common\models\c2\entity\ActivityPlayerVoteRecordModel;
use common\models\c2\entity\FeUserModel;
use common\models\c2\entity\GiftModel;
use common\models\c2\statics\FeUserType;
use common\models\c2\statics\VoteType;
use cza\base\models\statics\EntityModelStatus;
use yii\helpers\ArrayHelper;

class BrushVoteRecodeModel extends ActivityPlayerVoteRecordModel
{

    public $brush_sub_ids;
    // public $username;

    public function rules()
    {
        return ArrayHelper::merge(parent::rules(), [
            // [['brush_sub_ids'],'integer'],
            [['brush_sub_ids', 'user_id',], 'required'],
        ]);
    }

    public function loadDefaultValues($skipIfSet = true)
    {
        parent::loadDefaultValues($skipIfSet); // TODO: Change the autogenerated stub
        // $users = FeUserModel::find()->where([
        //     'type' => FeUserType::TYPE_SYS_REGISTRATION,
        //     'status' => EntityModelStatus::STATUS_ACTIVE
        // ])->asArray()->all();
        // $ids = ArrayHelper::getColumn($users, 'id');
        // $randomId = $ids[array_rand($ids)];
        // $user = FeUserModel::findOne($randomId);
        // $this->username = $user->username;
        // $this->user_id = $user->id;
    }

    public function attributeLabels()
    {
        return ArrayHelper::merge(parent::attributeLabels(), [
            'username' => \Yii::t('app.c2', 'Username')
        ]); // TODO: Change the autogenerated stub
    }

    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
        if ($insert) {
            foreach ($this->brush_sub_ids as $brush_sub_id) {
                if ($brush_sub_id == 0) {
                    $this->updateAttributes([
                        'type' => VoteType::TYPE_SYS_FREE,
                        'vote_number' => 1,
                    ]);
                    $this->player->updateCounters([
                        'free_vote_number' => 1,
                        'total_vote_number' => 1,
                        'view_number' => 1,
                    ]);
                }
                $model = GiftModel::findOne($brush_sub_id);
                if ($model) {
                    $this->updateAttributes([
                        'type' => VoteType::TYPE_SYS_GIFT,
                        'gift_id' => $model->id,
                        'gift_name' => $model->name,
                        'vote_number' => $model->obtain_vote_number,
                    ]);
                    $this->player->updateCounters([
                        'gift_vote_number' => $model->obtain_vote_number,
                        'total_vote_number' => $model->obtain_vote_number,
                        'view_number' => 1,
                    ]);
                }
                $this->player->activity->updateCounters([
                    'vote_number' => 1,
                    'view_number' => 1,
                ]);
            }
        }
    }

}